<?php


namespace api\controllers;


use api\components\BaseController;
use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Bulletin;
use common\models\Help;
use common\models\System;
use Yii;

class SystemController extends BaseController
{
    /**
     * 系统配置
     * @return array
     */
    public function actionIndex()
    {
        $delivery_method = DictCache::get('delivery_method');
        $astrict_sex = DictCache::get('astrict_sex');
        $withdraw_way = DictCache::get('withdraw_way');
        $system = System::getModel()->toArray();
        $system['logo'] = $system['logo']??'';
        $system['contact_phone'] = $system['contact_phone']??'';
        $system['forward_cover'] = $system['forward_cover']??'';
        $system['share_cover'] = $system['share_cover']??'';
        $system['run_withdraw_fee'] = $system['run_withdraw_fee']??0;
        $system['dis_withdraw_fee'] = $system['dis_withdraw_fee']??0;
        $delivery = [];
        foreach ($delivery_method as $key=>$item){
            $delivery[] = ['value'=>$key,'name'=>$item];
        }
        $astrict = [];
        foreach ($astrict_sex as $k=>$value){
            $astrict[] = ['value'=>$k,'name'=>$value];
        }
        $system['withdraw_way'] = explode(',',$system['withdraw_way']);
        $withdrawWayArr = [];
        foreach ($system['withdraw_way'] as $key=>$item){
            $withdrawWayArr[] = [
                'name'=>$withdraw_way[$item]??'',
                'value'=>$item,
            ];
        }
        $data = [
            'delivery_method' => $delivery,//配送方式
            'astrict_sex' => $astrict,//限制性别
            'theme_color' => $system['theme_color']??'',//主题色
            'logo' => commonApi::getDomain($system['logo']),//LOGO
            'contact_phone' => $system['contact_phone']??'',//联系方式
            'name' => $system['name']??'',//平台名称
            'forward_title' => $system['forward_title']??'',//转发标题
            'forward_cover' => commonApi::getDomain($system['forward_cover']),//转发封面
            'share_title' => $system['share_title']??'',//分享标题
            'share_cover' => commonApi::getDomain($system['forward_cover']),//分享封面
            'withdraw_explain' => $system['withdraw_explain']??'',//提现说明
            'min_withdraw' => $system['min_withdraw']??0,//最低提现金额
            'dis_agreement' => $system['dis_agreement']??'',//分销协议
            'runn_agreement' => $system['runn_agreement']??'',//跑腿协议
            'withdraw_way' => $withdrawWayArr,//提现方式
            'run_fee' => $system['run_withdraw_fee'].'%',//跑腿提现手续费
            'dis_fee' => $system['dis_withdraw_fee'].'%',//分销提现手续费
        ];
        return commonApi::message('ok',true,$data);
    }

    /**
     * 帮助中心
     * @return array
     */
    public function actionHelp()
    {
        $data = Help::find()
            ->where(['status'=>1])
            ->select(['id as h_id','title'])
            ->orderBy(['sort'=>SORT_DESC,'create_time'=>SORT_DESC])
            ->asArray()->all();
        return commonApi::message('ok',true,$data);
    }

    /**
     * 帮助详情
     * @return array
     */
    public function actionHelpDetail()
    {
        $h_id = Yii::$app->request->get('h_id','');
        $data = Help::find()->where(['id'=>$h_id])->select(['title','content'])->asArray()->one();
        return commonApi::message('ok',true,[
            'title'=>$data['title'],
            'content'=>$data['content'],
        ]);
    }

    /**
     * 公告
     * @return array
     */
    public function actionBulletin()
    {
        $data = Bulletin::find()
            ->where(['status'=>1])
            ->select(['id as b_id','title'])
            ->orderBy(['sort'=>SORT_DESC,'create_time'=>SORT_DESC])
            ->asArray()->all();
        return commonApi::message('ok',true,$data);
    }

    /**
     * 公告详情
     * @return array
     */
    public function actionBulletinDetail()
    {
        $b_id = Yii::$app->request->get('b_id','');
        $data = Bulletin::find()->where(['id'=>$b_id])->select(['title','content'])->asArray()->one();
        return commonApi::message('ok',true,[
            'title'=>$data['title'],
            'content'=>$data['content'],
        ]);
    }
}